package com.jjwxc.jwjskandriod.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.LoginActivity;
import com.jjwxc.jwjskandriod.activity.VipInfoActivity;
import com.jjwxc.jwjskandriod.framework.imageloader.FFImageLoader;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.widget.AutoHeightViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainBgFragment extends Fragment {
    private View view;
    TabLayout tabLayout;
    AutoHeightViewPager viewPager;
    List<Integer> list = new ArrayList<>();

    public MainBgFragment(AutoHeightViewPager viewPager) {
        this.viewPager = viewPager;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_mainbg, container, false);
            viewPager.setViewForPosition(view, 1);
            tabLayout = view.findViewById(R.id.tab_mainpg_layout);
            list.add(R.mipmap.mainpg_vip);
            list.add(R.mipmap.vip_bg_three);
            list.add(R.mipmap.vip_bg_two);
            setTabs(tabLayout, getLayoutInflater());
        }
        return view;
    }

    /**
     * 设置页卡显示效果
     *
     * @param tabLayout
     * @param inflater
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater) {
        for (int i = 0; i < 3; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.mainpg_item, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            ImageView img_head = (ImageView) view.findViewById(R.id.img_grid);
            FFImageLoader.loadImage(getActivity(), img_head, list.get(i), true);
            img_head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (PreUtils.getBoolean("tourist", false)) {
                        Intent intent = new Intent();
                        intent.setClass(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(getActivity(), VipInfoActivity.class);
                        intent.putExtra("tab","1");
                        startActivity(intent);
                    }
                }
            });
            tabLayout.addTab(tab);
        }
    }
}

